package com.mcxx.modules.temporary.service.impl;

import com.mcxx.modules.temporary.service.TempQueryService;
import com.mcxx.modules.miniSalvation.family.dto.MemberAssetsDTO;
import com.mcxx.modules.temporary.family.dto.TmpFamilyMemberDTO;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMemberEntity;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMemberHistoryEntity;
import com.mcxx.modules.temporary.family.service.read.TmpFamilyMemberHistoryReadService;
import com.mcxx.modules.temporary.family.service.read.TmpFamilyMemberReadService;
import com.mcxx.modules.temporary.order.dto.ApplicationDTO;
import com.mcxx.modules.temporary.order.entity.TmpOrderEntity;
import com.mcxx.modules.temporary.order.service.read.TmpOrderReadService;
import com.mcxx.util.Constant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TempQueryServiceImpl implements TempQueryService {

    @Autowired
    private TmpOrderReadService tmpOrderReadService;
    @Autowired
    private TmpFamilyMemberReadService familyMemberReadService;
    @Autowired
    private TmpFamilyMemberHistoryReadService familyMemberHistoryReadService;


    @Override
    public ApplicationDTO getOrderDetail(String orderId) {

        String bizState = tmpOrderReadService.getBizStateById((orderId));


        if (!Constant.HIS_ABLE_STATUS.contains(bizState)) {
            return tmpOrderReadService.getDetailById(orderId);
        }


        return tmpOrderReadService.getFinishedDetailById(orderId);
    }


    @Override
    public List listFamilyMember(String orderId) {

        TmpOrderEntity tmpOrderEntity = tmpOrderReadService.getFamilyIdAndBizStateById(orderId);


        if (!Constant.HIS_ABLE_STATUS.contains(tmpOrderEntity.getState())) {
            return familyMemberReadService.listByFamilyId(tmpOrderEntity.getFamilyId());
        }


        return familyMemberHistoryReadService.listByOrderId(orderId);
    }


    @Override
    public TmpFamilyMemberEntity getFamilyMember(String id, String orderId) {

        String state = tmpOrderReadService.getStateById((orderId));


        if (!Constant.HIS_ABLE_STATUS.contains(state)) {
            return familyMemberReadService.getById(id);
        }


        TmpFamilyMemberHistoryEntity memberHistoryEntity = familyMemberHistoryReadService.getById(id);
        TmpFamilyMemberEntity memberEntity = new TmpFamilyMemberEntity();
        BeanUtils.copyProperties(memberHistoryEntity, memberEntity);

        return memberEntity;
    }


    @Override
    public TmpFamilyMemberDTO getFamilyMemberBankInfo(String id, String orderId, String businessCode) {

        String state = tmpOrderReadService.getStateById((orderId));


        if (!Constant.HIS_ABLE_STATUS.contains(state)) {
            return familyMemberReadService.getByIdAndBusinessCode(id,businessCode);
        }


        return familyMemberHistoryReadService.getByIdAndBusinessCode(id,businessCode);
    }

}
